package com.wzx.selfuse.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wzx.selfuse.entity.PlanPro;
import com.wzx.selfuse.service.impl.Plan_IProServiceImpl;
import com.wzx.selfuse.vo.AjaxEntityResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @author Liu Tenda
 * @date 2022年 12月06日 19:58:10
 * Introduction:
 */
@Controller
@RequestMapping("/pro")
public class Plan_ProController {
    @Autowired
    Plan_IProServiceImpl plan_iProService;

//    产品列表
    @RequestMapping("/proList")
    public String getProList(HttpServletRequest req){
        Integer page=1;
        Integer num=5;
        if (req.getParameter("page")!=null){
            page = Integer.parseInt(req.getParameter("page"));
        }
        PageHelper.startPage(page,num);
        List<PlanPro> proList = plan_iProService.getProList();
        PageInfo<PlanPro> of=PageInfo.of(proList);
        req.setAttribute("proList",of.getList());
        req.setAttribute("proListOf",of);
        return "plan_pro";
    }

//    跳转添加产品页面
    @RequestMapping("/plan_pro_add")
    public String proAdd(){

        return "plan_pro_add";
    }

//    添加产品
    @PostMapping("/addPro")
    @ResponseBody
    public AjaxEntityResult addPro(@RequestBody PlanPro planPro){
        AjaxEntityResult ajaxEntityResult = new AjaxEntityResult();
        PlanPro proByProNumber = plan_iProService.getProByProNumber(planPro.getProNumber());
        if (proByProNumber!=null) {
                ajaxEntityResult.setCode(400);
                ajaxEntityResult.setMsg("编号已存在");
        } else {
            ajaxEntityResult.setCode(200);
            ajaxEntityResult.setMsg("添加成功");
            plan_iProService.addPro(planPro);
        }
        return ajaxEntityResult;
    }

//    删除产品
    @PostMapping("/deletePro")
    @ResponseBody
    public AjaxEntityResult deletePro(@RequestBody String proNumber){
        int i = plan_iProService.deletePro(proNumber);
        AjaxEntityResult ajaxEntityResult=new AjaxEntityResult();
        if (i==0){
            ajaxEntityResult.setCode(400);
            ajaxEntityResult.setMsg("删除时发生错误");
        }
        if (i==1){
            ajaxEntityResult.setCode(200);
            ajaxEntityResult.setMsg("删除成功");
        }
        return ajaxEntityResult;
    }

//    删除选中
    @PostMapping("/deleteChecked")
    @ResponseBody
    public AjaxEntityResult deleteChecked(@RequestBody List<String> proNumber){
        Iterator<String> iterator = proNumber.iterator();
        int a=0;//用于计数批量删除了多少条
        while (iterator.hasNext()){
            String next = iterator.next();
            int i = plan_iProService.deletePro(next);
            if (i==1){
                a++;
            }
        }
        AjaxEntityResult ajaxEntityResult=new AjaxEntityResult();
        if (a>0){
            ajaxEntityResult.setCode(200);
            ajaxEntityResult.setMsg("成功删除"+a+"条数据");
        }else {
            ajaxEntityResult.setCode(400);
            ajaxEntityResult.setMsg("删除失败，未选择产品或产品不存在");
        }
        return ajaxEntityResult;
    }

//    跳转到产品介绍页面
    @RequestMapping("/plan_pro_introduction")
    public String proIntroduction(HttpServletRequest req){
        String proNumber = req.getParameter("proNumber");
        PlanPro pro = plan_iProService.getProByProNumber(proNumber);
        String intro = pro.getIntro();
        req.setAttribute("intro",intro);
        return "plan_pro_introduction";
    }

    @RequestMapping("/plan_pro_edit")
    public String proEdit(HttpServletRequest req){
        String proNumber = req.getParameter("proNumber");
        PlanPro pro = plan_iProService.getProByProNumber(proNumber);
        req.setAttribute("pro",pro);
        return "plan_pro_edit";
    }

    @RequestMapping("/editPro")
    @ResponseBody
    public AjaxEntityResult editPro(@RequestBody PlanPro planPro){
        int i = plan_iProService.editPro(planPro);
        AjaxEntityResult result=new AjaxEntityResult();
        if (i==0){
            result.setCode(400);
            result.setMsg("修改失败");
        }
        if (i==1){
            result.setCode(200);
            result.setMsg("修改成功");
        }
        return result;
    }

    @RequestMapping("/checkPro")
    public String checkPro(String controller,String searchPro,HttpServletRequest req){
        List<PlanPro> pro=new ArrayList<>();
        switch (controller){
            case "1":
                PlanPro checkedPro = plan_iProService.getProByProNumber(searchPro);
                pro.add(checkedPro);
                break;
            case "2":
                pro = plan_iProService.getProByProName(searchPro);
                break;
            case "3":
                pro = plan_iProService.getProByProKind(searchPro);
                break;
            default:
                req.setAttribute("msg","找不到符合条件的产品");
        }
        req.setAttribute("proList",pro);
        return "plan_pro";
    }
    }
